<template>
<!-- 小韩自己设计的静态页面 -->
  <div class="spare-set-page">
<!--  form表单  -->
    <el-form :inline="true" :model="spareForm" class="demo-form-inline" style="border-bottom:1px solid #ccc;">
<!--   备件编码   -->
      <el-form-item :label="nowLanguage.spare+nowLanguage.code" class="selectMB10">
<!--    请输入备件编码信息    -->
        <el-input size="mini" v-model="spareForm.code" :placeholder="nowLanguage.pleaseImport+nowLanguage.spare+nowLanguage.code"></el-input>
      </el-form-item>
<!--备件名称-->
      <el-form-item :label="nowLanguage.spare+nowLanguage.name" class="selectMB10">
<!--    请输入备件名称信息    -->
        <el-input size="mini" v-model="spareForm.name" :placeholder="nowLanguage.pleaseImport+nowLanguage.spare+nowLanguage.name"></el-input>
      </el-form-item>
      <!--当前状态-->
      <el-form-item :label="nowLanguage.currentState" class="selectMB10">
        <el-select size="mini" clearable v-model="spareForm.status" :placeholder="nowLanguage.pleaseSelect" style="width:90px;" >
          <el-option v-for="(status,index) in statusLists" :key="index" :label="status.label" :value="status.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="selectMB10">
        <!--查询-->
        <el-button class="bolder outsideBtnBS selectAll" :disabled="selectDisabled" size="mini" type="danger" @click="querySpareSheet()" v-text="nowLanguage.query"></el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "SpareSet",
  data() {
    return {
      nowLanguage:{
        zh:{
          "pleaseSelect":"请选择",
          "pleaseImport":"请输入",
          "query":"查询",
          "code":"编码",
          "detailed":"查询",
          "name":"名称",
          "beijian":"备件",
          "xiaolei":"备件小类",
          "dalei":"计划大类",
          "farenName":"法人名称",
          "currentState":"当前状态",
          "spare":"备件小类",
          "parameterName":"参数名称",
          "spareParameterList":"备件参数明细",
          "create":"新增",
          "sequence":"序",
          "operate":"操作",
          "forbidden":"禁用",
          "all":"全部",
          "enable":"启用",
          "update":"修改",
          "parameterType":"参数类型",
          "paramterUnit":"参数单位",
          "founder":"创建人",
          "creationTime":"创建时间",
          "modifier":"修改人",
          "modificationTime":"修改时间",
          "parameterValue":"参数值",
          "spareParameterValue":"备件参数值",
          "status":"状态",
          "save":"保存",
          "cancel":"取消",
          "sure":"确定",
        },
        en:{
          "pleaseSelect":"please Select",
          "pleaseImport":"please Import",
          "query":"query",
          "spare":"spare spare",
          "parameterName":"parameter Name",
          "spareParameterList":"spare Parameter List",
          "create":"create",
          "sequence":"sequence",
          "operate":"operate",
          "forbidden":"forbidden",
          "enable":"enable",
          "update":"update",
          "parameterType":"parameter Type",
          "paramterUnit":"paramter Unit",
          "founder":"founder",
          "creationTime":"creation Time",
          "modifier":"modifier",
          "modificationTime":"modification Time",
          "parameterValue":"parameter Value",
          "spareParameterValue":"spare Parameter Value",
          "status":"status",
          "save":"save",
          "cancel":"cancel",
          "sure":"sure",

        }
      },
      // 查询表单
      spareForm:{
        name:"",
        code:"",
        status:"",
        remark:""
      },
      statusLists:[],   //当前状态下拉列表
      labelWidth:"100px",
      addspareForm:{
        name:"",
        code:"",
        status:true,
        spareId:"",
        planClassId:"",
        legalPersonCode:[],
        legalPersonId:[]
      },
      updatespareForm:{
        name:"",
        code:"",
        wareHouseBADropDown:[],
        legalPersonCode:"",
        legalPersonIds:[],
        legalPersonId:[],
//                    legalPersonid:[],
        legalPersonVO:[],
        status:false,
        spareId:"",
        planClassId:"",
        version:0  //verion版本控制
      },
      wareHouseBALists:[],   // 库区业务属性下拉列表
      spareData:[],
      loading:false,
      disabled:false,


      //弹窗
      dialogForm1Visible:false,
      dialogForm2Visible:false,
      // 分页
      isPaging:false,
      pageIndex:1,    //当前页（必传）
      pageSize :10,   //每页多少条
      totalNumber: 20,
      totalPage:2,

      //查询禁用
      selectDisabled:false,
      saveSSDisabled:false,    //新增弹窗中保存操作按钮的禁用
      saveUSSDisabled:false   //修改弹窗中保存操作按钮的禁用
    };
  },
  created:function(){
    var _this=this;
    _this.languageType=="zh"?_this.nowLangue=_this.language.zh:_this.nowLangue=_this.language.en;
  },
  computed:{
    // powerData(){
    //     return this.$store.state.powerData;
    // },
    languageType(){
      return this.$store.state.languageType;
    }
  },
  watch:{
    languageType(){
      var _this=this;
      _this.languageType=="zh"?_this.nowLangue=_this.language.zh:_this.nowLangue=_this.language.en;
    }
  },
  //进入页面立即运行此方法
  mounted: function () {
    var _this = this;
    _this.$store.commit('currentActive', 'spare');
    // //ajax前后查询按钮禁用
    // $(".selectAll").ajaxStart(function(){
    //     _this.selectDisabled=true;
    // });
    // $(".selectAll").ajaxStop(function(){
    //     _this.selectDisabled=false;
    // });
    _this.dropDownValue();// 当前状态下拉列表
    _this.planClassDropDownValue();//备件小类下拉框
    _this.spareDropDownValue();//备件小类下拉框
    _this.querySubclssSheet();    //表单查询
    _this.wareHouseBADropDown();   // 法人多选框
  },
  methods:{
    // 列表显示的状态转化
    stateChange(row) {
      if (row.status === 1) {
        return '启用'
      } else  {
        return '禁用'
      }
    },
    // 库区业务属性下拉列表
    wareHouseBADropDown(){
      var _this = this;
      $.ajax({
        type:'GET',
        url:_this.procurement_service+'legalPlant/ofCurrentUser',
        cache:false,
        // data:JSON.stringify(tmp),
        dataType:'JSON',
        contentType:"application/json;charset=utf-8",
        success:function(data){
          if(data.success){
            _this.wareHouseBALists=data.data;
          }else{
            _this.common.errorTip(data.message);
          }
        }
      })
    },


    spareDropDownValue(){
      var _this = this;
      $.ajax({
        type:'GET',
        url:_this.procurement_service+"spare/spareAll",
        cache:false,
        success:function(data){
          if(data.success){
            _this.spareDropDown = data.data;
          }else{
            _this.common.errorTip(data.message);
          }
        }
      })
    },
    planClassDropDownValue(){
      var _this = this;
      $.ajax({
        type:'GET',
        url:_this.procurement_service+"planCategory/planCategoryAll",
        cache:false,
        success:function(data){
          if(data.success){
            _this.planClassDropDown = data.data;
          }else{
            _this.common.errorTip(data.message);
          }
        }
      })
    },
    // 当前状态下来列表
    dropDownValue(){
      var _this=this;
      var tmp=["spare.spareStatus"];
      $.ajax({
        type:'POST',
        url:_this.metadata_service+'metadata/getPullDownList',
        cache:false,
        data:JSON.stringify(tmp),
        dataType:'JSON',
        contentType:"application/json;charset=utf-8",
        success:function(data){
          if(data.success){
            _this.statusLists=data.data.status;
          }else{
            _this.common.errorTip(data.message);
          }
        }
      })
    },
    //查询列表
    querySubclssSheet(){
      var _this=this;
      _this.loading=true;
      var tmp={
        name:_this.spareForm.name,
        code:_this.spareForm.code,
        status:_this.spareForm.status,
        pageIndex:_this.pageIndex,
        pageSize:_this.pageSize
      };
      _this.selectDisabled=true;
      $.ajax({
        type: 'POST',
        url: _this.metadata_service+"spare/queryPage",
        cache: false,
        data:JSON.stringify(tmp),
        dataType:'JSON',
        contentType: "application/json;charset=utf-8",
        success: function (data) {
          if (data.success) {
            _this.loading=false;
            _this.isPaging=true;
            _this.totalPage=data.data.totalPage;
            _this.totalNumber=data.data.totalNumber;
            _this.spareData=data.data.data;
          }else{
            _this.common.errorTip(data.message)
          }
          _this.selectDisabled=false;
        }
      });
    },
    //新增税码税率信息
    createNew(){
      var _this=this;
      _this.dialogForm1Visible=true;
      if(this.$refs['addspareForm']) {
        this.$refs['addspareForm'].resetFields();
      }
      _this.addspareForm.name="";
      _this.addspareForm.code="";
      _this.addspareForm.status=true;
      _this.addspareForm.remark="";
    },
    //新建保存
    savespare(){
      var _this=this;
      _this.$refs["addspareForm"].validate((valid) => {
        if (valid) {
          var tmp={
            name:_this.addspareForm.name,   //去除税码两边的空格
            code:_this.addspareForm.code,
            status:_this.addspareForm.status,
            spareId:_this.addspareForm.spareId,
            planClassId:_this.addspareForm.planClassId,
            legalPersonIds:_this.addspareForm.legalPersonId,
          };
          _this.saveSSDisabled=true;
          $.ajax({
            type: 'POST',
            url: "http://127.0.0.1:18082/spare/addSpare",
            cache: false,
            data:JSON.stringify(tmp),
            dataType:'JSON',
            contentType: "application/json;charset=utf-8",
            success: function (data) {
              if (data.success) {
                var success=_this.languageType=="zh"?'保存成功':'Save Success';
                _this.common.successTip(success);
                _this.dialogForm1Visible=false;
                _this.querySubclssSheet();
              }else{
                _this.common.errorTip(data.message);
              }
              _this.saveSSDisabled=false;
            }
          });
        } else {
          var  err=_this.common.formErrorInfo();
          _this.common.errorTip(err);
          return false;
        }
      });
    },
    //新建取消
    cancelspare(){
      var _this=this;
      _this.dialogForm1Visible=false;
      if(this.$refs['addspareForm']) {
        this.$refs['addspareForm'].resetFields();
      }
      _this.addspareForm.name="";
      _this.addspareForm.code="";
      _this.addspareForm.remark="";
      _this.addspareForm.status=false;
      _this.addspareForm.remark="";
    },
    // 操作启用禁用
    handleStatus(index, row){
      var _this=this;
      if(row.status === 1){
        row.status = 0;
      }else{
        row.status = 1;
      }
      var tmp={id: row.id,status: row.status};
      $.ajax({
        type: 'POST',
        url: "http://127.0.0.1:18082/spare/updateStatus",
        cache: false,
        data:JSON.stringify(tmp),
        dataType:'JSON',
        contentType: "application/json;charset=utf-8",
        success: function (data) {
          if (data.success) {
            // 当前状态的显示
            if(row.status){
              row.statusName = "启用";
              var success=_this.languageType=="zh"?'启用成功':'Enable success';
              _this.common.successTip(success);
            }else{
              row.statusName = "禁用";
              var success=_this.languageType=="zh"?'禁用成功':'Disable success';
              _this.common.successTip(success);
            }
          }else
            _this.common.errorTip(data.message);
        }
      });
    },

    //点击修改
    updateCorFact(index,row){
      var _this=this;
      _this.dialogForm2Visible=true;
      _this.handleGetData(row.id);
      if(this.$refs['updatespareForm']) {
        this.$refs['updatespareForm'].resetFields();
      }
      _this.updatespareForm.id = row.id;
      _this.updatespareForm.name = row.spareName;
      _this.updatespareForm.code = row.spareCode;
      _this.updatespareForm.status = row.status;
      _this.updatespareForm.spareId = row.spareChildId;
      _this.updatespareForm.planClassId = row.planCategoryId;
      _this.updatespareForm.legalPersonIds = row.legalPersonId;
    },
    // 修改查看取值
    handleGetData(id){
      var _this = this;
      $.ajax({
        url:'http://127.0.0.1:18082/spare/queryById?id='+id,
        type:'GET',
        cache:false,
        // data:JSON.stringify(tmp),
        dataType:'JSON',
        contentType:'application/json;charset=utf-8',
        success:function(data){
          if(data.success){
            var bAArr = data.data.data;
            var tmpBA = [];
            // 库区业务属性
            for(var i=0;i<bAArr.length;i++){
              tmpBA.push(bAArr[i].updatespareForm.legalPersonName);
            }
//                            if(_this.dialogForm2Visible){
            _this.updatespareForm = data.data.data;
            _this.updatespareForm.legalPersonVO = tmpBA;
            _this.seedWareHouseBA(tmpBA);   // 库区业务属性
//                            }
            /* if(_this.dialogForm3Visible){
                 _this.seeWarehouseAreaForm = data.data;
                 _this.seeWarehouseAreaForm.businessAttribute = tmpBA;
             }*/
          }else{
            _this.common.errorTip(data.message);
          }
        }
      })

    },
    //新增弹窗中 选择库区业务属性
    selectedWareHouseBA(value){
      var _this = this;
      _this.seedWareHouseBA(value);
    },
    // 库区业务属性
    seedWareHouseBA(value){
      var _this = this;
      var businessAttributeValue=[],selectedValue=[];
      for(var i=0;i<value.length;i++){
        for(var j=0;j<_this.wareHouseBALists.length;j++){
          if(value[i] == _this.wareHouseBALists[j].legalPersonName){
            businessAttributeValue.push(_this.wareHouseBALists[j].id);
          }
        }
      }
      for(var k=0;k<businessAttributeValue.length;k++){
        selectedValue.push({
          legalPersonVO:businessAttributeValue[k]
        });
      }
//                if(_this.dialogForm1Visible){
//                    _this.addWarehouseAreaForm.checkedBusinessAttribute = selectedValue;
//                }else if(_this.dialogForm2Visible){
      _this.updatespareForm.legalPersonVO = selectedValue;
//                }
    },
    //修改保存
    saveUpdatespare(){
      var _this=this;
      _this.$refs["updatespareForm"].validate((valid) => {
        if (valid) {
          var tmp={
            id:_this.updatespareForm.id,
            name:_this.updatespareForm.name,
            code:_this.updatespareForm.code,
            spareId: _this.updatespareForm.spareId,
            planClassId: _this.updatespareForm.planClassId,
            status:_this.updatespareForm.status,
            legalPersonId:_this.updatespareForm.legalPersonId,//verion版本控制
            version:_this.updatespareForm.version
          };
          _this.saveUSSDisabled=true;
          $.ajax({
            type: 'POST',
            url: "http://127.0.0.1:18082/spare/updateSpare",
            cache: false,
            data:JSON.stringify(tmp),
            dataType:'JSON',
            contentType: "application/json;charset=utf-8",
            success: function (data) {
              if (data.success) {
                var success=_this.languageType=="zh"?'修改成功！':'Modify successfully';
                _this.common.successTip(success);
                _this.dialogForm2Visible=false;
                _this.querySubclssSheet();
              }else{
                _this.common.errorTip(data.message);
              }
              _this.saveUSSDisabled=false;
            }
          });
        } else {
          var  err=_this.common.formErrorInfo();
          _this.common.errorTip(err);
          return false;
        }
      });
    },
    //修改取消
    cancleUpdatespare(){
      var _this=this;
      _this.dialogForm2Visible=false;
    },
    //   分页
    handleSizeChange(val) {
      var _this=this;
      _this.pageSize=val;
      _this.querySubclssSheet();

    },
    //点击页码分页查询点击事件
    handleCurrentChange(val) {
      var _this=this;
      _this.pageIndex=val;
      _this.querySubclssSheet();
    }
  }
}
</script>

<style scoped>

</style>